/**
 * @Author yinyunqi
 * @date 2018年1月31日
 * @Content 
 */
package com.yongqi.config;

import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.yongqi.task.ScheduleTask;

@Configuration
public class QuartzConfigration {
    
    /**
     * 配置定时任务
     */
    @Bean(name="jobDetail")
    public MethodInvokingJobDetailFactoryBean detailFactoryBean(ScheduleTask task) {
	MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
	jobDetail.setConcurrent(false);		//是否并发执行
	jobDetail.setName("email-task");	//任务名字
	jobDetail.setGroup("email");		//设置任务分组
	jobDetail.setTargetObject(task);	//为需要执行的实体类对应的对象
	jobDetail.setTargetMethod("sendEmail");
	return jobDetail;
    }
    
    /**
     * 配置定时任务触发器：何时触发执行定时任务
     */
    @Bean(name="jobTrigger")
    public CronTriggerFactoryBean cronTrigger(MethodInvokingJobDetailFactoryBean jobDetail) {
	CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
	tigger.setJobDetail(jobDetail.getObject());
	tigger.setCronExpression("0 0 14 * * ?"); 	//初始cron表达式
	tigger.setName("email-task");
	return tigger;
    }
    /**
     * 定义quartz调度工厂
     */
    @Bean(name="scheduler")
    public SchedulerFactoryBean schedulerFactoryBean(Trigger cronJobTrigger) {
	SchedulerFactoryBean bean = new SchedulerFactoryBean();
	bean.setOverwriteExistingJobs(true);		//用于quartz集群，QuartzScheduler启动时更新已存在的Job
	bean.setStartupDelay(1);		//延时启动,应用启动一秒后
	bean.setTriggers(cronJobTrigger);
	return bean;
    }
    
}

